package lb.io.client;

import io.grpc.Channel;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import lb.io.api.core.v1.CarrySpec;
import lb.io.lblet.pkg.service.pb.*;

public class LbLetClient {
    private final Channel channel;

    public LbLetClient(Channel channel) {
        this.channel = channel;
    }

    public String pingz() throws StatusRuntimeException {
        PingzRequest request = PingzRequest.newBuilder().build();
        PingzResponse response = LbletGrpc.newBlockingStub(channel).pingz(request);
        return response.getMessage();
    }

    public StreamObserver<BackupResponse> backup(CarrySpec spec)
            throws StatusRuntimeException {

        LbletGrpc.LbletStub stub = LbletGrpc.newStub(channel);
        StreamObserver<BackupResponse> responseObserver = new StreamObserver<BackupResponse>() {
            @Override
            public void onNext(BackupResponse response) {
                System.out.println(response.getOut());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        };

        BackupRequest request = BackupRequest.newBuilder().setBody(spec).build();
        StreamObserver<BackupRequest> requestObserver = stub.backup(responseObserver);
        requestObserver.onNext(request);
        // requestObserver.onCompleted();
        return responseObserver;
    }
}
